#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int x=0;bool f=0;char ch=getchar();
	while(ch<'0'||ch>'9')f^=(ch=='-'),ch=getchar();
	while('0'<=ch&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int Maxn=5005,mod=998244353;
int n,a[Maxn];
int f[Maxn][Maxn];
int b[Maxn];
int l[Maxn],r[Maxn];
stack<int>stk;
inline bool check(int i,int j){
	j=b[j];
	if(l[j]<=i&&i<=j)return 1;
	return 0;
}
int sum[Maxn];
int main(){
	freopen("after.in","r",stdin);
	freopen("after.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++){
		a[i]=read();b[a[i]]=i;
		while(stk.size()&&a[stk.top()]<a[i]){
			r[stk.top()]=i-1;
			stk.pop();
		}
		if(stk.empty())l[i]=1;
		else l[i]=stk.top()+1;
		stk.push(i);
	}
	while(stk.size())r[stk.top()]=n,stk.pop();
	for(int j=a[1];j<=n;j++){
		if(check(1,j)){
			f[1][j]=1;
		}
	}f[1][a[1]]=1;
//	for(int j=1;j<=n;j++)
//		printf("f[%d][%d]=%d\n",1,j,f[1][j]);
//	puts("");
	for(int i=2;i<=n;i++){
		for(int j=a[i];j<=n;j++){
			if(check(i,j)){
				for(int k=1;k<=j;k++)f[i][j]=(f[i-1][k]+f[i][j])%mod;
				for(int k=j+1;k<=n;k++)
					if(b[k]<=i)f[i][j]=(f[i][j]+f[i-1][k])%mod;
			}else{
				f[i][j]=f[i-1][j];
				if(b[j]<=i){
					for(int k=j+1;k<=n;k++)
						if(b[k]<i)f[i][j]=(f[i][j]+f[i-1][k])%mod;
				}
			}
		}
//		for(int j=1;j<=n;j++)
//			printf("f[%d][%d]=%d\n",i,j,f[i][j]);
//		puts("");
	}
	int res=0;
	for(int i=1;i<=n;i++)res=(res+f[n][i])%mod;
	printf("%d\n",res);
	return 0;
}
/*
5
1 3 2 5 4

8
4 7 5 1 2 6 3 8 

4
4 2 1 3
*/
